package com.cheys.webapp.util;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

public class PingThread implements Runnable {
	private HttpClient client;
	private String url;
	
	public PingThread(String url) {
		super();
		LogUtil.getLogger(PingThread.class).debug("create thread...");
		this.client = new HttpClient();
		this.client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
		this.url = url;
	}

	public void run() {
		// TODO Auto-generated method stub
		PostMethod method = new PostMethod(url);
		for(;;){
		try {
				LogUtil.getLogger(PingThread.class).debug("start request createHTML");
				int executeMethod = client.executeMethod(method);
				Thread.yield();
				if(executeMethod == HttpStatus.SC_OK){
					LogUtil.getLogger(getClass()).debug("end request,JAVA WEB SERVICE RUNNING...");
					break;
				}else{
					Thread.sleep(3000);
					LogUtil.getLogger(PingThread.class).debug("request createHTML fail,挂起3秒钟");
				}
		} catch (Exception e) {
			LogUtil.getLogger(PingThread.class).debug("request createHTML Exception:"+e);
		} finally{
			try {
				Thread.yield();
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				LogUtil.getLogger(PingThread.class).debug("Thread sleep Exception:"+e);
			}
		}
		}
	}

}
